package com.xinjiang.electricpower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.xinjiang.electricpower.domain.DTO.WorkOrderPoolDTO;
import com.xinjiang.electricpower.domain.DTO.XjOrderGeneralDTO;
import com.xinjiang.electricpower.domain.XjOrderGeneral;
import com.xinjiang.electricpower.domain.vo.*;
import com.xinjiang.electricpower.enums.ElectricPowerEnums;
import com.xinjiang.electricpower.mapper.SysOrgMapper;
import com.xinjiang.electricpower.mapper.XjOrderGeneralMapper;
import com.xinjiang.electricpower.mapper.XjOrderSourceMapper;
import com.xinjiang.electricpower.service.IUserAuthService;
import com.xinjiang.electricpower.service.IXjOrderGeneralService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工单处理实体类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class XjOrderGeneralServiceImpl extends ServiceImpl<XjOrderGeneralMapper, XjOrderGeneral> implements IXjOrderGeneralService {


    private final IUserAuthService iUserAuthService;
    private final XjOrderSourceMapper xjOrderSourceMapper;
    private final TokenService tokenService;
    private final SysOrgMapper sysOrgMapper;
    private final XjOrderGeneralMapper xjOrderGeneralMapper;

    /**
     * 自定义分页
     *
     * @param page              分页对象
     * @param xjOrderGeneralDTO 工单实体类
     * @return IPage<XjOrderGeneralVO> 返回工单实体类待处理分页信息
     */
    @Override
    public PageInfo<XjOrderGeneralVO> selectXjOrderGeneralPage(PageDTO<XjOrderGeneralVO> page, XjOrderGeneralDTO xjOrderGeneralDTO) {
        QueryWrapper<XjOrderGeneralVO> wrapper = getXjOrderGeneralVOQueryWrapper(xjOrderGeneralDTO);
        if ("1".equals(xjOrderGeneralDTO.getStatus())) {
            AuthInfoVo authByDaiBan = iUserAuthService.getAuthByDaiBan();
            wrapper.in(ObjectUtils.isNotEmpty(authByDaiBan.getConsNo()), "xog.cons_no",authByDaiBan.getConsNo());
        }else{
            AuthInfoVo authStr = iUserAuthService.getAuthStr();
            wrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());  // 当前单位下的所有工单
            wrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo()); // 给据当前用户
        }
        //开启分页功能
        PageMethod.startPage(Integer.parseInt(String.valueOf(page.getCurrent())), Integer.parseInt(String.valueOf(page.getSize())));
        return new PageInfo<>(baseMapper.findXjOrderGenerals(wrapper));
    }

    /**
     * 自定义分页
     *
     * @param page             分页对象
     * @param workOrderPoolDTO 工单池DTO类
     * @return PageInfo<WorkOrderPoolVO> 返回工单池实体类待处理分页信息
     */
    @Override
    public PageInfo<WorkOrderPoolVO> selectXjOrderGeneralPoolPage(PageDTO<WorkOrderPoolVO> page, WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<WorkOrderPoolVO> wrapper = new QueryWrapper<>();//daf
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        //wrapper.eq(ObjectUtils.isNotEmpty(workOrderPoolDTO.getTimeGranularityMonth()), "DATE_FORMAT(xog.dispatch_time,'%Y-%m')", ObjectUtils.isNotEmpty(workOrderPoolDTO.getTimeGranularityMonth()) ? workOrderPoolDTO.getTimeGranularityMonth().format(dateTimeFormatter) : null);
        wrapper.like(StringUtils.hasText(workOrderPoolDTO.getGeneralOrderNo()), "xog.order_no", workOrderPoolDTO.getGeneralOrderNo());
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getTgId()), "xog.tg_id", workOrderPoolDTO.getTgId());
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getTgName()), "xog.tg_name", workOrderPoolDTO.getTgName());
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getPriority()), "xoc.priority", workOrderPoolDTO.getPriority());
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getSystemId()), "xos.id", workOrderPoolDTO.getSystemId());
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getNature()), "xoc.nature", workOrderPoolDTO.getNature());
        String column = "xog.type";
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getType()), column, workOrderPoolDTO.getType());
        wrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateStart());
        wrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateEnd());
        wrapper.eq(StringUtils.hasText(workOrderPoolDTO.getStatus()), "xog.status", workOrderPoolDTO.getStatus());
        wrapper.isNotNull("xog.org_no");
        wrapper.isNotNull("xog.cons_no");
        setWorkOrderAuthParam(workOrderPoolDTO);//权限参数设置
        wrapper.eq(StringUtils.isNotEmpty(workOrderPoolDTO.getOrgNo()),"xog.org_no",workOrderPoolDTO.getOrgNo());
        wrapper.in(ObjectUtils.isNotEmpty(workOrderPoolDTO.getConsNo()),"xog.cons_no",workOrderPoolDTO.getConsNo());
        if (ObjectUtils.isNotEmpty(workOrderPoolDTO.getMultipleStatus())) {
            wrapper.in("xog.status", Arrays.asList(workOrderPoolDTO.getMultipleStatus().split(",")));
        }
        wrapper.orderByDesc("xoc.priority");
        PageMethod.startPage(Integer.parseInt(String.valueOf(page.getCurrent())), Integer.parseInt(String.valueOf(page.getSize())));
        return new PageInfo<WorkOrderPoolVO>(baseMapper.findXjOrderPoolGenerals(wrapper));
    }


    /**
     * 工单池列表
     *
     * @param page
     * @param workOrderPoolDTO
     * @return
     */
    @Override
    public PageInfo<WorkOrderPoolVO> selectXjOrdralPoolPerGeneageTest(PageDTO<WorkOrderPoolVO> page, WorkOrderPoolDTO workOrderPoolDTO) {
        PageMethod.startPage(Integer.parseInt(String.valueOf(page.getCurrent())), Integer.parseInt(String.valueOf(page.getSize())));
         setWorkOrderAuthParam(workOrderPoolDTO);//权限参数设置
        return new PageInfo<WorkOrderPoolVO>(baseMapper.findXjOrderPoolGeneralsTest(workOrderPoolDTO));
    }

    @NotNull
    private QueryWrapper<XjOrderGeneralVO> getXjOrderGeneralVOQueryWrapper(XjOrderGeneralDTO xjOrderGeneralDTO) {
        QueryWrapper<XjOrderGeneralVO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(xjOrderGeneralDTO.getOrderNo()), "xog.order_no", xjOrderGeneralDTO.getOrderNo());
        wrapper.eq(StringUtils.hasText(xjOrderGeneralDTO.getStatus()), "xog.status", xjOrderGeneralDTO.getStatus());
        wrapper.eq(StringUtils.hasText(xjOrderGeneralDTO.getSystemId()), "xos.id", xjOrderGeneralDTO.getSystemId());
        wrapper.eq(StringUtils.hasText(xjOrderGeneralDTO.getOrderType()), "xog.type", xjOrderGeneralDTO.getOrderType());
        wrapper.ge(ObjectUtils.isNotEmpty(xjOrderGeneralDTO.getBeginDateStart()), "CONVERT(xog.begin_date, date)", xjOrderGeneralDTO.getBeginDateStart());
        wrapper.le(ObjectUtils.isNotEmpty(xjOrderGeneralDTO.getBeginDateEnd()), "CONVERT(xog.begin_date, date)", xjOrderGeneralDTO.getBeginDateEnd());
        wrapper.eq(ObjectUtils.isNotEmpty(xjOrderGeneralDTO.getDispatchTime()), "CONVERT(xog.dispatch_time, date)", xjOrderGeneralDTO.getDispatchTime());
        wrapper.like(StringUtils.hasText(xjOrderGeneralDTO.getTgName()), "xog.tg_name", xjOrderGeneralDTO.getTgName());
        wrapper.isNotNull("xog.org_no");
        wrapper.isNotNull("xog.cons_no");
        wrapper.groupBy("xog.id");
        return wrapper;
    }

    /**
     * 查询工单列表无分页
     *
     * @param xjOrderGeneralDTO 通用工单DTO类
     * @return List<XjOrderGeneralVO> 返回工单列表
     */
    @Override
    public List<XjOrderGeneralVO> selectXjOrderGenerals(XjOrderGeneralDTO xjOrderGeneralDTO) {
        return baseMapper.findXjOrderGenerals(this.getXjOrderGeneralVOQueryWrapper(xjOrderGeneralDTO));
    }

    /**
     * 查询工单列表无分页
     *
     * @param ids 批量id
     * @return List<XjOrderGeneralVO> 返回工单列表
     */
    @Override
    public List<XjOrderGeneralVO> selectXjOrderGenerals(String... ids) {
        QueryWrapper<XjOrderGeneralVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("xog.id", Arrays.asList(ids));
        return baseMapper.findXjOrderGenerals(queryWrapper);
    }

    /**
     * 查询工单列表无分页
     *
     * @param ids 批量id
     * @return List<WorkOrderPoolVO> 返回工单池列表
     */
    @Override
    public List<WorkOrderPoolVO> selectWorkOrderPools(String... ids) {
        QueryWrapper<WorkOrderPoolVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("xog.id", Arrays.asList(ids));
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        return baseMapper.findXjOrderPoolGenerals(queryWrapper);
    }

    /**
     * 查询工单池列表无分页
     *
     * @param workOrderPoolDTO 工单池DTO类
     * @return List<WorkOrderPoolVO> 返回工单列表
     */
    @Override
    public List<WorkOrderPoolVO> selectWorkOrderPools(WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<WorkOrderPoolVO> objectQueryWrapper = new QueryWrapper<>();
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        objectQueryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        objectQueryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        return baseMapper.findXjOrderPoolGenerals(new QueryWrapper<>());
    }

    /**
     * 查询各个台区下的工单数
     *
     * @param workOrderPoolDTO 工单池DTO类
     * @return List<TgWorkOrderStatisticVO> Tg台区工单列表
     */
    @Override
    public List<TgWorkOrderStatisticVO> selectXjOrderGenerals(WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<TgWorkOrderStatisticVO> queryWrapper = new QueryWrapper<>();
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        //queryWrapper.eq("DATE_FORMAT(xog.dispatch_time, '%Y-%m')", workOrderPoolDTO.getTimeGranularityMonth().format(dateTimeFormatter));
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        queryWrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateEnd());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getSystemId()), "xos.id", workOrderPoolDTO.getSystemId());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getType()), "xog.type", workOrderPoolDTO.getType());
        List<String> list = new ArrayList<>();
        list.add("xog.tg_id");
        list.add("xog.tg_name");
        queryWrapper.groupBy(list);
        return baseMapper.countTgOrders(queryWrapper);
    }
    /**
     * 指定台区查询工单统计
     *
     * @param workOrderPoolDTO
     * @return
     */
    @Override
    public List<TgWorkOrderStatisticVO> selectXjOrderGeneralsTest(WorkOrderPoolDTO workOrderPoolDTO) {
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        return xjOrderGeneralMapper.selectXjOrderGeneralsTest(workOrderPoolDTO,authStr);
    }

    /**
     * 查询各个工单预警类型下的工单数
     *
     * @param workOrderPoolDTO 工单池DTO类
     * @return List<TgWorkOrderStatisticVO> 统计各个预警类型工单数
     */
    @Override
    public List<WarningUrgentVO> selectWorkOrderWarning(WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<EventProcessingVO> queryWrapper = new QueryWrapper<>();
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        //queryWrapper.eq("DATE_FORMAT(xog.dispatch_time, '%Y-%m')", workOrderPoolDTO.getTimeGranularityMonth().format(dateTimeFormatter));
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"atyx.org_no",authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"atyx.cons_no",authStr.getConsNo());
        queryWrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(atyx.handle_time,date)", workOrderPoolDTO.getBeginDateStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(atyx.handle_time,date)", workOrderPoolDTO.getBeginDateEnd());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getSystemId()), "atyx.sys_id", workOrderPoolDTO.getSystemId());
//        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getType()), "xog.type", workOrderPoolDTO.getType());
//        queryWrapper.groupBy("xoc.priority");
//        queryWrapper.orderByDesc("xoc.priority");
        queryWrapper.groupBy("atyx.app_yjdj");
        queryWrapper.orderByDesc("atyx.app_yjdj");
        return baseMapper.countPriorityOrders(queryWrapper);
    }

    /**
     * 查询指定年月各个系统下的工单数
     *
     * @param workOrderPoolDTO 工单池DTO类
     * @return List<TgWorkOrderStatisticVO> 统计各个预警类型工单数
     */
    @Override
    public List<SystemOrdersVO> selectWorkOrderSystem(WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<SystemOrdersVO> queryWrapper = new QueryWrapper<>();
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getSystemId()), "xos.id", workOrderPoolDTO.getSystemId());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getType()), "xog.type", workOrderPoolDTO.getType());
        //queryWrapper.eq("DATE_FORMAT(xog.dispatch_time, '%Y-%m') ", workOrderPoolDTO.getTimeGranularityMonth().format(dateTimeFormatter));
        queryWrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateEnd());
        queryWrapper.groupBy("xos.id");

        List<SystemOrdersVO> systemOrdersVOS = baseMapper.countSystemOrderCondition(queryWrapper);
        if (CollectionUtils.isNotEmpty(systemOrdersVOS)) {
            systemOrdersVOS.forEach(systemOrdersVO -> {
                QueryWrapper<XjOrderGeneralVO> queryWrap = new QueryWrapper<XjOrderGeneralVO>();
                queryWrap.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
                queryWrap.eq(StringUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
                queryWrap.eq(StringUtils.hasText(workOrderPoolDTO.getSystemId()), "xos.id", workOrderPoolDTO.getSystemId());
                queryWrap.eq(StringUtils.hasText(workOrderPoolDTO.getType()), "xog.type", workOrderPoolDTO.getType());
                queryWrap.isNotNull("xog.type");
                queryWrap.groupBy("xog.type");
                queryWrap.orderByDesc("num");
                //查询该系统下的工单类型统计
                List<OrderTypeStatisticsVO> orderTypeStatisticsVOS = baseMapper.countOrderTypeBySystem(queryWrap);
                systemOrdersVO.setList(orderTypeStatisticsVOS);
            });
            return systemOrdersVOS;
        }
        return systemOrdersVOS;
    }

    /**
     * 统计指定年月各个系统/业务的工单数
     *
     * @param workOrderPoolDTO
     * @return
     */
    @Override
    public List<SystemOrdersVO> selectWorkOrderSystemTest(WorkOrderPoolDTO workOrderPoolDTO) {
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        List<SystemOrdersVO> systemOrdersVOS = xjOrderGeneralMapper.countSystemOrderTest(workOrderPoolDTO, authStr);
        if (CollectionUtils.isNotEmpty(systemOrdersVOS)) {
            systemOrdersVOS.forEach(systemOrdersVO -> {
                //查询该系统下的工单类型统计
                List<OrderTypeStatisticsVO> orderTypeStatisticsVOS = baseMapper.countOrderTypeBySystemTest(workOrderPoolDTO,authStr,null);
                systemOrdersVO.setList(orderTypeStatisticsVOS);
            });
            return systemOrdersVOS;
        }
        return systemOrdersVOS;
    }

    /**
     * 查询指定年月各个工单性质下的工单数
     *
     * @param workOrderPoolDTO 工单池DTO类
     * @return List<WorkOrderNatureStatisticVO> 查询指定年月各个工单性质下的工单数
     */
    @Override
    public List<WorkOrderNatureStatisticVO> selectWorkOrderNature(WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<WorkOrderNatureStatisticVO> queryWrapper = new QueryWrapper<>();
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        //queryWrapper.eq("DATE_FORMAT(xog.dispatch_time, '%Y-%m') ", workOrderPoolDTO.getTimeGranularityMonth().format(dateTimeFormatter));
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        queryWrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateEnd());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getSystemId()), "xos.id", workOrderPoolDTO.getSystemId());
        queryWrapper.eq(StringUtils.hasText(workOrderPoolDTO.getType()), "xog.type", workOrderPoolDTO.getType());
        queryWrapper.groupBy("xog.business_type");
        return baseMapper.countNatureOrders(queryWrapper);
    }

    /**
     * 查询总数
     *
     * @return Long
     */
    @Override
    public Long countOrderGeneral() {
        LambdaQueryWrapper<XjOrderGeneral> queryWrapper = new LambdaQueryWrapper<>();
        //执行中跟已完结的数量
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),XjOrderGeneral::getOrgNo,authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),XjOrderGeneral::getConsNo,authStr.getConsNo());
        queryWrapper.and(item -> item.eq(XjOrderGeneral::getStatus, 2).or().eq(XjOrderGeneral::getStatus, 3));
        queryWrapper.isNotNull(XjOrderGeneral::getOrgNo);
        queryWrapper.isNotNull(XjOrderGeneral::getConsNo);
        long count = this.count(queryWrapper);
        queryWrapper.clear();
        //待办数量
        AuthInfoVo authByDaiBan = iUserAuthService.getAuthByDaiBan();
        queryWrapper.in(ObjectUtils.isNotEmpty(authByDaiBan.getConsNo()),XjOrderGeneral::getConsNo,authByDaiBan.getConsNo());
        queryWrapper.eq(XjOrderGeneral::getStatus, 1);
        queryWrapper.isNotNull(XjOrderGeneral::getOrgNo);
        queryWrapper.isNotNull(XjOrderGeneral::getConsNo);
        long count1 = this.count(queryWrapper);
        return count + count1;
    }

    /**
     * 查询待办工单总数
     *
     * @return Long
     */
    @Override
    public Long countOrderGeneralByStatus() {
        LambdaQueryWrapper<XjOrderGeneral> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XjOrderGeneral::getStatus, ElectricPowerEnums.ORDER_STATUS_PROCESS.getValue());
        return this.count(queryWrapper);
    }


    /**
     * 查询各个系统下的工单数
     *
     * @return List<SystemOrdersVO> 返回各个系统下的工单数
     */
    @Override
    public List<SystemOrdersVO> countSystemOrders() {
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        QueryWrapper<SystemOrdersVO> sysWrapper = new QueryWrapper<SystemOrdersVO>();
        sysWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        sysWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        sysWrapper.isNotNull("xog.org_no");
        sysWrapper.isNotNull("xog.cons_no");
        sysWrapper.groupBy("xoc.source_id");
        sysWrapper.and(item -> item.eq("xog.status", 2).or().eq("xog.status", 3));
        List<SystemOrdersVO> systemOrders = baseMapper.countSystemOrders(sysWrapper);
        //待办
        sysWrapper.clear();
        AuthInfoVo authByDaiBan = iUserAuthService.getAuthByDaiBan();
        sysWrapper.in(ObjectUtils.isNotEmpty(authByDaiBan.getConsNo()),"xog.cons_no",authByDaiBan.getConsNo());
        sysWrapper.isNotNull("xog.org_no");
        sysWrapper.isNotNull("xog.cons_no");
        sysWrapper.groupBy("xoc.source_id");
        sysWrapper.eq("xog.status", 1);
        List<SystemOrdersVO> systemOrders1 = baseMapper.countSystemOrders(sysWrapper);
        //查询所有系统来源，默认工单数量为0
        List<SystemOrdersVO> systemOrdersList =  xjOrderSourceMapper.selectSourceName();
        for (SystemOrdersVO systemOrdersVO : systemOrdersList) {
            systemOrdersVO.setOrderNum(0L);
            for (SystemOrdersVO vo : systemOrders) {
                if (systemOrdersVO.getId().equals(vo.getId())) {
                    systemOrdersVO.setOrderNum(vo.getOrderNum());
                }
            }
            for (SystemOrdersVO vo : systemOrders1) {
                if (systemOrdersVO.getId().equals(vo.getId())) {
                    systemOrdersVO.setOrderNum(systemOrdersVO.getOrderNum() + vo.getOrderNum());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(systemOrdersList)) {
            systemOrdersList.forEach(systemOrdersVO -> {
                if (systemOrdersVO.getOrderNum() > 0) {
                    QueryWrapper<XjOrderGeneralVO> queryWrapper = new QueryWrapper<XjOrderGeneralVO>();
                    queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
                    queryWrapper.eq(StringUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
                    queryWrapper.eq("xos.id", systemOrdersVO.getId());
                    queryWrapper.and(item -> item.eq("xog.status", 2).or().eq("xog.status", 3));
                    queryWrapper.isNotNull("xog.org_no");
                    queryWrapper.isNotNull("xog.cons_no");
                    queryWrapper.groupBy("xog.type");
                    queryWrapper.orderByDesc("num");
                    //查询该系统下的工单类型统计
                    List<OrderTypeStatisticsVO> orderTypeStatisticsVOS = baseMapper.countOrderTypeBySystem(queryWrapper);
                    //待办
                    queryWrapper.clear();
                    queryWrapper.eq(StringUtils.isNotEmpty(authByDaiBan.getConsNo()),"xog.cons_no",authByDaiBan.getConsNo());
                    queryWrapper.eq("xos.id", systemOrdersVO.getId());
                    queryWrapper.eq("xog.status", 1);
                    queryWrapper.isNotNull("xog.org_no");
                    queryWrapper.isNotNull("xog.cons_no");
                    queryWrapper.groupBy("xog.type");
                    queryWrapper.orderByDesc("num");
                    //查询该系统下的工单类型统计
                    List<OrderTypeStatisticsVO> orderTypeStatisticsVOS1 = baseMapper.countOrderTypeBySystem(queryWrapper);
                    for (OrderTypeStatisticsVO vo : orderTypeStatisticsVOS) {
                        for (OrderTypeStatisticsVO vo1 : orderTypeStatisticsVOS1) {
                            if (vo.getType().equals(vo1.getType())) {
                                vo.setNum(vo.getNum() + vo1.getNum());
                            }
                        }
                    }
                    systemOrdersVO.setList(orderTypeStatisticsVOS);
                }
            });
            return systemOrdersList;
        }
        return systemOrdersList;
    }

    /**
     * 查询各个系统下的工单数
     *
     * @param workOrderPoolDTO 工单池DTO类
     * @return Map<String, Long>
     */
    @Override
    public List<SystemOrdersVO> countSystemOrders(WorkOrderPoolDTO workOrderPoolDTO) {
        QueryWrapper<SystemOrdersVO> wrapper = new QueryWrapper<>();
        wrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateStart());
        wrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateEnd());
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        wrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
        wrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
        wrapper.isNotNull("xog.type");
        wrapper.groupBy("xoc.source_id");
        List<SystemOrdersVO> systemOrders = baseMapper.countSystemOrder(wrapper);
        //查询所有系统来源，默认工单数量为0
        List<SystemOrdersVO> systemOrdersList =  xjOrderSourceMapper.selectSourceName();
        for (SystemOrdersVO systemOrdersVO : systemOrdersList) {
            systemOrdersVO.setOrderNum(0L);
            for (SystemOrdersVO vo : systemOrders) {
                if (systemOrdersVO.getId().equals(vo.getId())) {
                    systemOrdersVO.setOrderNum(vo.getOrderNum());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(systemOrdersList)) {
            //AtomicReference<Long> orderNum = new AtomicReference<>(0L);
            systemOrdersList.forEach(systemOrdersVO -> {
                if (systemOrdersVO.getOrderNum() > 0) {
                    //orderNum.updateAndGet(v -> v + systemOrdersVO.getOrderNum());
                    QueryWrapper<XjOrderGeneralVO> queryWrapper = new QueryWrapper<XjOrderGeneralVO>();
                    queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),"xog.org_no",authStr.getOrgNo());
                    queryWrapper.eq(StringUtils.isNotEmpty(authStr.getConsNo()),"xog.cons_no",authStr.getConsNo());
                    queryWrapper.eq("xos.id", systemOrdersVO.getId());
                    queryWrapper.isNotNull("xog.type");
                    queryWrapper.ge(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateStart()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateStart());
                    queryWrapper.le(ObjectUtils.isNotEmpty(workOrderPoolDTO.getBeginDateEnd()), "CONVERT(xog.begin_date,date)", workOrderPoolDTO.getBeginDateEnd());
                    queryWrapper.groupBy("xog.type");
                    queryWrapper.orderByDesc("num");
                    //查询该系统下的工单类型统计
                    List<OrderTypeStatisticsVO> orderTypeStatisticsVOS = baseMapper.countOrderTypeBySystem(queryWrapper);
                    systemOrdersVO.setList(orderTypeStatisticsVOS);
                }
            });
            return systemOrdersList;
        }

        return systemOrdersList;
    }


    /**
     * 工单业务占比
     *
     * @param workOrderPoolDTO
     * @return
     */
    @Override
    public List<SystemOrdersVO> countSystemOrdersTest(WorkOrderPoolDTO workOrderPoolDTO) {
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        List<SystemOrdersVO> systemOrders = baseMapper.countSystemOrderTest(workOrderPoolDTO,authStr);
        //查询所有系统来源，默认工单数量为0
        List<SystemOrdersVO> systemOrdersList =  xjOrderSourceMapper.selectSourceName();
        for (SystemOrdersVO systemOrdersVO : systemOrdersList) {
            systemOrdersVO.setOrderNum(0L);
            for (SystemOrdersVO vo : systemOrders) {
                if (systemOrdersVO.getId().equals(vo.getId())) {
                    systemOrdersVO.setOrderNum(vo.getOrderNum());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(systemOrdersList)) {
            //AtomicReference<Long> orderNum = new AtomicReference<>(0L);
            systemOrdersList.forEach(systemOrdersVO -> {
                if (systemOrdersVO.getOrderNum() > 0) {
                    //查询该系统下的工单类型统计
                    List<OrderTypeStatisticsVO> orderTypeStatisticsVOS = baseMapper.countOrderTypeBySystemTest(workOrderPoolDTO,authStr, systemOrdersVO.getId());
                    systemOrdersVO.setList(orderTypeStatisticsVOS);
                }
            });
            return systemOrdersList;
        }
        return systemOrdersList;
    }

    /**
     * 查询待办工单数
     *
     * @return Long
     */
    @Override
    public Long countToDoOrder() {
        LambdaQueryWrapper<XjOrderGeneral> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XjOrderGeneral::getStatus, ElectricPowerEnums.ORDER_STATUS_PROCESS.getValue());
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        queryWrapper.eq(StringUtils.isNotEmpty(authStr.getOrgNo()),XjOrderGeneral::getOrgNo,authStr.getOrgNo());
        queryWrapper.in(ObjectUtils.isNotEmpty(authStr.getConsNo()),XjOrderGeneral::getConsNo,authStr.getConsNo());
        queryWrapper.isNotNull(XjOrderGeneral::getType);
        return this.count(queryWrapper);
    }


    private void setWorkOrderAuthParam(WorkOrderPoolDTO workOrderPoolDTO){
        AuthInfoVo authStr = iUserAuthService.getAuthStr();
        workOrderPoolDTO.setOrgNo(authStr.getOrgNo());
        workOrderPoolDTO.setConsNo(authStr.getConsNo());
    }
}

